package de.dopichaj.labrador.tools;


import java.io.EOFException;
import java.io.IOException;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import de.dopichaj.labrador.index.backend.DiskTermFrequencyIterator;
import de.dopichaj.labrador.index.backend.TermFrequency;
import de.dopichaj.labrador.index.backend.TermFrequencyInputStream;
import de.dopichaj.labrador.util.io.BitInputStream;


public final class DiskInvertedListStats {

    public static final class Distribution {
        
        final List<Integer> dist = new ArrayList<Integer>();
        int count = 0;
        final int maxValue;
        
        /**
         * 
         * @param maxValue
         *      The maximum value to store; all values greater than this value
         *      are treated as if they were equal to it.
         */
        public Distribution(final int maxValue) {
            this.maxValue = maxValue;
        }
        
        public void add(int value) {
            if (value > maxValue) {
                value = maxValue;
            }
            while (dist.size() <= value) {
                dist.add(0);
            }
            dist.set(value, dist.get(value) + 1);
            count++;
        }
        
        public List<Integer> getDistribution() {
            return dist;
        }
        
        public void print(final PrintStream writer) {

            double sum = 0;
            for (int i = 0; i < dist.size(); i++) {
                final int f = dist.get(i);
                final double relF = f/(double)count;
                sum += relF;
                writer.printf("%d\t%d\t%g\t%g\n", i, f, relF, sum);
            }

        }
    }
    
    public static void main(String[] args) throws IOException {
        
        if (args.length < 1) {
            System.exit(-1);
        }
        final String baseName = args.length > 1 ? args[1] : "stats";
        final RandomAccessFile file = new RandomAccessFile(args[0], "r");
        final BitInputStream input = new BitInputStream(file);
        
        int termID = 0;
        long lastPos = 0;
        int globalCount = 0;
        final Distribution lenDist = new Distribution(100000);
        final Distribution freqDist = new Distribution(100000);
        final Distribution docIDDist = new Distribution(100000);
        try {
            while (true) {
                final TermFrequencyInputStream tfInput = new TermFrequencyInputStream(
                    input, true, termID);
                final DiskTermFrequencyIterator iterator = new DiskTermFrequencyIterator(tfInput);
                
                int count = 0;
                int lastID = 0;
                while (iterator.hasNext()) {
                    final TermFrequency freq = iterator.next();
                    freqDist.add(freq.getFrequency());
                    docIDDist.add(freq.getDocumentID() - lastID);
                    lastID = freq.getDocumentID();
                    count++;
                }
                
                lenDist.add(count);
                termID++;
                input.skipToNext();
                if (count != 0) {
                    final long newPos = file.getFilePointer();
                    final long bytes = newPos - lastPos;
                    final double avgBytes = (double)bytes / count;
                    System.out.printf("count\t%d\tlen\t%d\t%g\n", count, bytes, avgBytes);
                    lastPos = newPos;
                }
                globalCount += count;
            }
        } catch (EOFException e) {
            
            System.err.println("global count: " + globalCount);
        }

        freqDist.print(new PrintStream(baseName + "-freqdist.dat"));
        docIDDist.print(new PrintStream(baseName + "-dociddist.dat"));
        lenDist.print(new PrintStream(baseName + "-lendist.dat"));
    }

}/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
